package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.android_program.data_class.movie_1_1
import com.example.android_program.data_class.ticket_seat
import com.example.myapplication.ui.movie.Cinema_ChooseTimeSlot
import com.example.myapplication.ui.movie.SelectCinemaActivity
import kotlinx.android.synthetic.main.activity_buy_ticket.*


import java.lang.StringBuilder

class BuyTicketActivity : AppCompatActivity() {
    val seat_info: ArrayList<ticket_seat> = ArrayList()
    var ticket_num:Int = 0
    var ticket_price:Double = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_buy_ticket)
//        Toast.makeText(this,"${}",Toast.LENGTH_LONG).show()

        //初始化界面信息
        val cinema_name = intent.getStringExtra("cinema_name")
        val movie_name = intent.getStringExtra("movie_name")
        val date = intent.getStringExtra("date")
        val start_time = intent.getStringExtra("start_time")
        val end_time = intent.getStringExtra("end_time")
        val language = intent.getStringExtra("language")
        val hall = intent.getStringExtra("hall")
        ticket_price = intent.getDoubleExtra("price", 0.0)

        choise_cinema.text = cinema_name
        choise_moviename.text = movie_name
        choise_date.text = date
        choise_time.text = "${start_time}~${end_time}"
        choise_kind.text = language

        confirm_seat.setOnClickListener {
            var seat_String:String = seat_info.get(0).seat_info

            for (i in 1 until seat_info.size){
                seat_String = "${seat_String},${seat_info.get(i).seat_info}"
            }



            val intent = Intent(this, PayActivity::class.java)
            intent.putExtra("ticket_num", ticket_num)
            intent.putExtra("ticket_price", ticket_price)
            intent.putExtra("movie_name", movie_name)
            intent.putExtra("date", date)
            intent.putExtra("language", language)
            intent.putExtra("cinema_name", cinema_name)
            intent.putExtra("start_time", start_time)
            intent.putExtra("hall", hall)

            intent.putExtra("seat", seat_String)
            startActivity(intent)
        }

        set_1_1.setOnClickListener { Addchange(set_1_1,seat_info) }
        set_1_2.setOnClickListener { Addchange(set_1_2,seat_info) }
        set_1_3.setOnClickListener { Addchange(set_1_3,seat_info) }
        set_1_4.setOnClickListener { Addchange(set_1_4,seat_info) }
        set_1_5.setOnClickListener { Addchange(set_1_5,seat_info) }
        set_1_6.setOnClickListener { Addchange(set_1_6,seat_info) }
        set_1_7.setOnClickListener { Addchange(set_1_7,seat_info) }
        set_1_8.setOnClickListener { Addchange(set_1_8,seat_info) }
        set_1_9.setOnClickListener { Addchange(set_1_9,seat_info) }

        set_2_1.setOnClickListener { Addchange(set_2_1,seat_info) }
        set_2_2.setOnClickListener { Addchange(set_2_2,seat_info) }
        set_2_3.setOnClickListener { Addchange(set_2_3,seat_info) }
        set_2_4.setOnClickListener { Addchange(set_2_4,seat_info) }
        set_2_5.setOnClickListener { Addchange(set_2_5,seat_info) }
        set_2_6.setOnClickListener { Addchange(set_2_6,seat_info) }
        set_2_7.setOnClickListener { Addchange(set_2_7,seat_info) }
        set_2_8.setOnClickListener { Addchange(set_2_8,seat_info) }
        set_2_9.setOnClickListener { Addchange(set_2_9,seat_info) }

        set_3_1.setOnClickListener { Addchange(set_3_1,seat_info) }
        set_3_2.setOnClickListener { Addchange(set_3_2,seat_info) }
        set_3_3.setOnClickListener { Addchange(set_3_3,seat_info) }
        set_3_4.setOnClickListener { Addchange(set_3_4,seat_info) }
        set_3_5.setOnClickListener { Addchange(set_3_5,seat_info) }
        set_3_6.setOnClickListener { Addchange(set_3_6,seat_info) }
        set_3_7.setOnClickListener { Addchange(set_3_7,seat_info) }
        set_3_8.setOnClickListener { Addchange(set_3_8,seat_info) }
        set_3_9.setOnClickListener { Addchange(set_3_9,seat_info) }

        set_4_1.setOnClickListener { Addchange(set_4_1,seat_info) }
        set_4_2.setOnClickListener { Addchange(set_4_2,seat_info) }
        set_4_3.setOnClickListener { Addchange(set_4_3,seat_info) }
        set_4_4.setOnClickListener { Addchange(set_4_4,seat_info) }
        set_4_5.setOnClickListener { Addchange(set_4_5,seat_info) }
        set_4_6.setOnClickListener { Addchange(set_4_6,seat_info) }
        set_4_7.setOnClickListener { Addchange(set_4_7,seat_info) }
        set_4_8.setOnClickListener { Addchange(set_4_8,seat_info) }

        set_5_1.setOnClickListener { Addchange(set_5_1,seat_info) }
        set_5_2.setOnClickListener { Addchange(set_5_2,seat_info) }
        set_5_3.setOnClickListener { Addchange(set_5_3,seat_info) }
        set_5_4.setOnClickListener { Addchange(set_5_4,seat_info) }
        set_5_5.setOnClickListener { Addchange(set_5_5,seat_info) }
        set_5_6.setOnClickListener { Addchange(set_5_6,seat_info) }
        set_5_7.setOnClickListener { Addchange(set_5_7,seat_info) }
        set_5_8.setOnClickListener { Addchange(set_5_8,seat_info) }
        set_5_9.setOnClickListener { Addchange(set_5_9,seat_info) }

        set_6_0.setOnClickListener { Addchange(set_6_0,seat_info) }
        set_6_1.setOnClickListener { Addchange(set_6_1,seat_info) }
        set_6_2.setOnClickListener { Addchange(set_6_2,seat_info) }
        set_6_3.setOnClickListener { Addchange(set_6_3,seat_info) }
        set_6_4.setOnClickListener { Addchange(set_6_4,seat_info) }
        set_6_5.setOnClickListener { Addchange(set_6_5,seat_info) }
        set_6_6.setOnClickListener { Addchange(set_6_6,seat_info) }
        set_6_7.setOnClickListener { Addchange(set_6_7,seat_info) }
        set_6_8.setOnClickListener { Addchange(set_6_8,seat_info) }
        set_6_9.setOnClickListener { Addchange(set_6_9,seat_info) }

        set_7_0.setOnClickListener { Addchange(set_7_0,seat_info) }
        set_7_1.setOnClickListener { Addchange(set_7_1,seat_info) }
        set_7_2.setOnClickListener { Addchange(set_7_2,seat_info) }
        set_7_3.setOnClickListener { Addchange(set_7_3,seat_info) }
        set_7_4.setOnClickListener { Addchange(set_7_4,seat_info) }
        set_7_5.setOnClickListener { Addchange(set_7_5,seat_info) }
        set_7_6.setOnClickListener { Addchange(set_7_6,seat_info) }
        set_7_7.setOnClickListener { Addchange(set_7_7,seat_info) }
        set_7_8.setOnClickListener { Addchange(set_7_8,seat_info) }
        set_7_9.setOnClickListener { Addchange(set_7_9,seat_info) }

        choise_back.setOnClickListener {
            val intent = Intent(this, Cinema_ChooseTimeSlot::class.java)
            setResult(3, intent)
            finish()
        }
    }
    //--------------------------------------------------------------------------------------
    private fun Addchange(set_x_x:ImageButton,seat_info:ArrayList<ticket_seat>){
        var temp_String:String = get_setString(set_x_x) //获得座位字符串
        Toast.makeText(this, "set_x_x.id.toString = ${set_x_x.id.toString()}--temp_string = ${temp_String}", Toast.LENGTH_LONG).show()
        var temp = ticket_seat(temp_String, ticket_price.toString())

        if(seat_info.indexOf(temp) == -1){   //第一次点击 不存在时
            set_x_x.setImageResource(R.drawable.set_seat)
            seat_info.add(temp)
            ticket_num++
        }
        else{//第二次点击
            set_x_x.setImageResource(R.drawable.unset_seat)
            seat_info.remove(temp)
            ticket_num--
        }
        refreshHistory()
    }

    private fun get_setString(set_x_x:ImageButton):String{
        var temp_String:String
        if(set_x_x.id.toString() == "2131231180")
            temp_String = "1排9座"
        else if(set_x_x.id.toString() == "2131231179")
            temp_String = "1排8座"
        else if(set_x_x.id.toString() == "2131231178")
            temp_String = "1排7座"
        else if(set_x_x.id.toString() == "2131231177")
            temp_String = "1排6座"
        else if(set_x_x.id.toString() == "2131231176")
            temp_String = "1排5座"
        else if(set_x_x.id.toString() == "2131231175")
            temp_String = "1排4座"
        else if(set_x_x.id.toString() == "2131231174")
            temp_String = "1排3座"
        else if(set_x_x.id.toString() == "2131231173")
            temp_String = "1排2座"
        else if(set_x_x.id.toString() == "2131231172")
            temp_String = "1排1座"
        else if(set_x_x.id.toString() == "2131231181")
            temp_String = "2排1座"
        else if(set_x_x.id.toString() == "2131231182")
            temp_String = "2排2座"
        else if(set_x_x.id.toString() == "2131231183")
            temp_String = "2排3座"
        else if(set_x_x.id.toString() == "2131231184")
            temp_String = "2排4座"
        else if(set_x_x.id.toString() == "2131231153")
            temp_String = "2排5座"
        else if(set_x_x.id.toString() == "2131231154")
            temp_String = "2排6座"
        else if(set_x_x.id.toString() == "2131231155")
            temp_String = "2排7座"
        else if(set_x_x.id.toString() == "2131231156")
            temp_String = "2排8座"
        else if(set_x_x.id.toString() == "2131231157")
            temp_String = "2排9座"
        else if(set_x_x.id.toString() == "2131231158")
            temp_String = "3排1座"
        else if(set_x_x.id.toString() == "2131231159")
            temp_String = "3排2座"
        else if(set_x_x.id.toString() == "2131231160")
            temp_String = "3排3座"
        else if(set_x_x.id.toString() == "2131231161")
            temp_String = "3排4座"
        else if(set_x_x.id.toString() == "2131231162")
            temp_String = "3排5座"
        else if(set_x_x.id.toString() == "2131231163")
            temp_String = "3排6座"
        else if(set_x_x.id.toString() == "2131231164")
            temp_String = "3排7座"
        else if(set_x_x.id.toString() == "2131231165")
            temp_String = "3排8座"
        else if(set_x_x.id.toString() == "2131231166")
            temp_String = "3排9座"
        else if(set_x_x.id.toString() == "2131231167")
            temp_String = "4排1座"
        else if(set_x_x.id.toString() == "2131231168")
            temp_String = "4排2座"
        else if(set_x_x.id.toString() == "2131231169")
            temp_String = "4排3座"
        else if(set_x_x.id.toString() == "2131231170")
            temp_String = "4排4座"
        else if(set_x_x.id.toString() == "2131231171")
            temp_String = "4排5座"
        else if(set_x_x.id.toString() == "2131231172")
            temp_String = "4排6座"
        else if(set_x_x.id.toString() == "2131231173")
            temp_String = "4排7座"
        else if(set_x_x.id.toString() == "2131231174")
            temp_String = "4排8座"
        else if(set_x_x.id.toString() == "2131231175")
            temp_String = "5排1座"
        else if(set_x_x.id.toString() == "2131231176")
            temp_String = "5排2座"
        else if(set_x_x.id.toString() == "2131231177")
            temp_String = "5排3座"
        else if(set_x_x.id.toString() == "2131231178")
            temp_String = "5排4座"
        else if(set_x_x.id.toString() == "2131231179")
            temp_String = "5排5座"
        else if(set_x_x.id.toString() == "2131231180")
            temp_String = "5排6座"
        else if(set_x_x.id.toString() == "2131231181")
            temp_String = "5排7座"
        else if(set_x_x.id.toString() == "2131231182")
            temp_String = "5排8座"
        else if(set_x_x.id.toString() == "2131231183")
            temp_String = "5排9座"
        else if(set_x_x.id.toString() == "2131231184")
            temp_String = "6排0座"
        else if(set_x_x.id.toString() == "2131231185")
            temp_String = "6排1座"
        else if(set_x_x.id.toString() == "2131231186")
            temp_String = "6排2座"
        else if(set_x_x.id.toString() == "2131231187")
            temp_String = "6排3座"
        else if(set_x_x.id.toString() == "2131231188")
            temp_String = "6排4座"
        else if(set_x_x.id.toString() == "2131231189")
            temp_String = "6排5座"
        else if(set_x_x.id.toString() == "2131231190")
            temp_String = "6排6座"
        else if(set_x_x.id.toString() == "2131231191")
            temp_String = "6排7座"
        else if(set_x_x.id.toString() == "2131231192")
            temp_String = "6排8座"
        else if(set_x_x.id.toString() == "2131231193")
            temp_String = "6排9座"
        else if(set_x_x.id.toString() == "2131231194")
            temp_String = "7排0座"
        else if(set_x_x.id.toString() == "2131231195")
            temp_String = "7排1座"
        else if(set_x_x.id.toString() == "2131231196")
            temp_String = "7排2座"
        else if(set_x_x.id.toString() == "2131231197")
            temp_String = "7排3座"
        else if(set_x_x.id.toString() == "2131231198")
            temp_String = "7排4座"
        else if(set_x_x.id.toString() == "2131231199")
            temp_String = "7排5座"
        else if(set_x_x.id.toString() == "2131231200")
            temp_String = "7排6座"
        else if(set_x_x.id.toString() == "2131231201")
            temp_String = "7排7座"
        else if(set_x_x.id.toString() == "2131231202")
            temp_String = "7排8座"
        else if(set_x_x.id.toString() == "2131231203")
            temp_String = "7排9座"
        else
            temp_String = "null"

        return temp_String
    }
    //--------------------------------------------------------------------------------------
    private fun refreshHistory(){
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        ticketrecyclerview.layoutManager = layoutManager
        val adapter = Ticket_info_Adapter(seat_info)
        ticketrecyclerview.adapter = adapter
    }
    //-----------------------------------------------------------------------------------------
    private class Ticket_info_ViewHolder(view: View): RecyclerView.ViewHolder(view){
        var ticket_position: TextView = view.findViewById(R.id.ticket_position)
        var ticket_money: TextView = view.findViewById(R.id.ticket_money)
    }

    private class Ticket_info_Adapter(val seat_info:List<ticket_seat>): RecyclerView.Adapter<Ticket_info_ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Ticket_info_ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_seatstring,parent,false)
            val viewHolder = Ticket_info_ViewHolder(view)
//            viewHolder.ticket_delete.setOnClickListener {
//                val position = viewHolder.adapterPosition
//                val info = seat_info[position]
//                var temp_seat = ticket_seat(info.seat_info,info.price)
//            }
            return viewHolder
        }

        override fun onBindViewHolder(holder: Ticket_info_ViewHolder, position: Int) {  //列表数据与链表数据互通
            val info = seat_info[position]
            holder.ticket_position.text = "${info.seat_info}"
            holder.ticket_money.text = "${info.price}"
        }

        override fun getItemCount(): Int {
            return seat_info.size
        }
    }
}